package com.sgcc.ln.pmos.e_ln_personalcenter.fragment;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ActivityUtils;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.sgcc.ln.pmos.arouter.app.AppArouterPath;
import com.sgcc.ln.pmos.arouter.order.OrderArouterPath;
import com.sgcc.ln.pmos.arouter.personalcenter.PersonalCenterArouterPath;
import com.sgcc.ln.pmos.common.base.BaseFragment;
import com.sgcc.ln.pmos.common.bean.EventCenter;
import com.sgcc.ln.pmos.common.utils.ArguConstant;
import com.sgcc.ln.pmos.common.utils.DataCleanManager;
import com.sgcc.ln.pmos.common.utils.DialogUtil;
import com.sgcc.ln.pmos.common.utils.UserManager;
import com.sgcc.ln.pmos.common.utils.VisibilConfig;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.ln.pmos.e_ln_personalcenter.activity.EstimatePowerChangeActivity;
import com.sgcc.ln.pmos.e_ln_personalcenter.activity.TermAgreementActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class UserFragment extends BaseFragment {

    private RelativeLayout mRlUser;
    private ImageView mIvHeadPortrait;
    private TextView mTvNoLogin;
    private TextView mTvNickname;
    private TextView mTvPhone;
    private RelativeLayout mRlCollection;
    private RelativeLayout mRlFocus;
    private RelativeLayout mRlFootprint;
    private RelativeLayout mRlLogout;
    private RelativeLayout mRlMessage;

    private RelativeLayout mRlQygl;
    private RelativeLayout mRlZhyaq;
    private RelativeLayout rlEstimatePowerChange;
    private RelativeLayout mRlAboutus;
    private RelativeLayout mRlYszc;
    private RelativeLayout mRlYjfk;
    private RelativeLayout mRlClearCache;
    private TextView mTvCacheNumber;
    private LinearLayout linnear_center;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_personalcenter;
    }

    @Override
    protected void initViews() {
        super.initViews();
        findViewByIds();

    }

    @Override
    public void onResume() {
        super.onResume();
        onChangePage();
    }

    private void onChangePage() {
        if (UserManager.isLogin()) {
            //登录成功的布局
            mTvNoLogin.setVisibility(View.GONE);
            mTvNickname.setVisibility(View.VISIBLE);
            mTvPhone.setVisibility(View.VISIBLE);
            mRlLogout.setVisibility(View.VISIBLE);

            mTvNickname.setText(UserManager.getUserBean().getNickname());
            if (UserManager.getUserBean().getTelPhone() != null && UserManager.getUserBean().getTelPhone().size() != 0) {
                String phone = UserManager.getUserBean().getTelPhone().get(0).replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
                mTvPhone.setText(phone);
            } else {
                mTvPhone.setText("");
            }
        } else {
            //未登录的布局
            mTvNoLogin.setVisibility(View.VISIBLE);
            mTvNickname.setVisibility(View.GONE);
            mTvPhone.setVisibility(View.GONE);
            mRlLogout.setVisibility(View.GONE);
        }
    }

    @Override
    public boolean isBindEventBusHere() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EventCenter eventCenter) {
        if (eventCenter.getEventCode() == ArguConstant.LOGOUT) {
            onChangePage();
        }
    }


    private void findViewByIds() {
        mRlUser = (RelativeLayout) mRootView.findViewById(R.id.rl_user);
        mIvHeadPortrait = (ImageView) mRootView.findViewById(R.id.iv_head_portrait);
        mTvNoLogin = (TextView) mRootView.findViewById(R.id.tv_no_login);
        mTvNickname = (TextView) mRootView.findViewById(R.id.tv_nickname);
        mTvPhone = (TextView) mRootView.findViewById(R.id.tv_phone);
        mRlCollection = (RelativeLayout) mRootView.findViewById(R.id.rl_collection);
        mRlFocus = (RelativeLayout) mRootView.findViewById(R.id.rl_focus);
        mRlFootprint = (RelativeLayout) mRootView.findViewById(R.id.rl_footprint);
        mRlLogout = (RelativeLayout) mRootView.findViewById(R.id.rl_logout);
        mRlMessage = (RelativeLayout) mRootView.findViewById(R.id.rl_message);


        mRlQygl = (RelativeLayout) mRootView.findViewById(R.id.rl_qygl);
        mRlZhyaq = (RelativeLayout) mRootView.findViewById(R.id.rl_zhyaq);
        rlEstimatePowerChange = (RelativeLayout) mRootView.findViewById(R.id.rlEstimatePowerChange);
        mRlAboutus = (RelativeLayout) mRootView.findViewById(R.id.rl_aboutus);
        mRlYszc = (RelativeLayout) mRootView.findViewById(R.id.rl_yszc);
        mRlYjfk = (RelativeLayout) mRootView.findViewById(R.id.rl_yjfk);
        mRlClearCache = (RelativeLayout) mRootView.findViewById(R.id.rl_clear_cache);
        mTvCacheNumber = (TextView) mRootView.findViewById(R.id.tv_cache_number);
        linnear_center = (LinearLayout) mRootView.findViewById(R.id.linnear_center);
        if (!VisibilConfig.isShowVisibil()) {
            linnear_center.setVisibility(View.GONE);
            mRlMessage.setVisibility(View.INVISIBLE);
        }


        try {
            mTvCacheNumber.setText(DataCleanManager.getTotalCacheSize(mContext));
        } catch (Exception e) {
            e.printStackTrace();
        }

        rlEstimatePowerChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //预计用电量变更
                if (UserManager.toLogin()) {
                    ActivityUtils.startActivity(EstimatePowerChangeActivity.class);
                }

            }
        });
        mRlQygl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //自定义曲线管理
                if (UserManager.toLogin()) {
                    ARouter.getInstance()
                            .build(PersonalCenterArouterPath.PERSONALCENTER_CUSTOMCURVEMANAGEMENT)
                            .navigation();
                }
            }
        });
        mRlZhyaq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //账号与安全
                if (UserManager.toLogin()) {
                    ARouter.getInstance()
                            .build(PersonalCenterArouterPath.PERSONALCENTER_HISTORICALPOWER)
                            .navigation();
                }
            }
        });
        mRlAboutus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //关于我们
                ARouter.getInstance()
                        .build(PersonalCenterArouterPath.PERSONALCENTER_ABOUTUS)
                        .navigation();
            }
        });
        mRlYszc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                ActivityUtils.startActivity(TermAgreementActivity.class);
            }
        });
        mRlYjfk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //意见反馈
                ARouter.getInstance()
                        .build(PersonalCenterArouterPath.PERSONALCENTER_FEEDBACK)
                        .navigation();
            }
        });

        mRlClearCache.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //清除缓存
                DialogUtil.showConfirm(getContext(), "提示", "确认要删除缓存吗?", new OnConfirmListener() {
                    @Override
                    public void onConfirm() {
                        //清除缓存
                        DataCleanManager.clearAllCache(mContext);
                        try {
                            mTvCacheNumber.setText(DataCleanManager.getTotalCacheSize(mContext));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });

            }
        });

        mRlUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!UserManager.isLogin()) {
                    ARouter.getInstance()
                            .build(AppArouterPath.APP_LOGIN)
                            .navigation();
                }
            }
        });

        mRlCollection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //收藏
                if (UserManager.toLogin()) {
                    ARouter.getInstance()
                            .build(PersonalCenterArouterPath.PERSONALCENTER_COLLECTION)
                            .navigation();
                }

            }
        });

        mRlFocus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //关注
                if (UserManager.toLogin()) {
                    ARouter.getInstance()
                            .build(PersonalCenterArouterPath.PERSONALCENTER_FOCUS)
                            .navigation();
                }
            }
        });

        mRlFootprint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //足迹
                if (UserManager.toLogin()) {
                    ARouter.getInstance()
                            .build(PersonalCenterArouterPath.PERSONALCENTER_FOOTPRINT)
                            .navigation();
                }

            }
        });

        mRlLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UserManager.logout();
                EventBus.getDefault().post(new EventCenter<>(ArguConstant.LOGOUT));
            }
        });

        mRlMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance()
                        .build(PersonalCenterArouterPath.PERSONALCENTER_MESSAGE)
                        .navigation();
            }
        });
    }
}
